package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MTerminalDiscountMgtExcution;
import com.fortune.dto.in.mTerminalDiscount.TerminalDiscountAddDto;
import com.fortune.dto.in.mTerminalDiscount.TerminalDiscountEditDto;
import com.fortune.dto.in.mTerminalDiscount.TerminalDiscountListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MTerminalDiscountMgtEnum;
import com.fortune.service.MTerminalDiscountService;
import com.fortune.vo.terminalDiscount.MTerminalDiscount;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "channelDiscountMgt", description = "终端优惠政策管理", tags = {"终端优惠政策管理"})
@RestController
@RequestMapping("terminalDiscountMgt")
public class MTerminalDiscountMgtController {
	@Autowired
	private MTerminalDiscountService mTerminalDiscountService;
	
	@ApiOperation(value = "优惠政策列表", nickname = "terminalDiscountList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="order", value="排序依据(起始日期:startTime,截止日期:endTime,满数量:achieveNum,赠数量:giftNum,优惠名称:name,商品名称:productName,商品编号:producNumber,默认为startTime)",allowableValues = "startTime,endTime,achieveNum,giftNum,name,productName,producNumber",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
	@GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<MTerminalDiscount>> list(HttpServletRequest request, @ApiIgnore @Validated TerminalDiscountListDto terminalDiscountListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		terminalDiscountListDto.setPage((terminalDiscountListDto.getPage()-1)*10);
		List<MTerminalDiscount> channelDiscountList = mTerminalDiscountService.queryTerminalDiscountList(companyId,terminalDiscountListDto);
		int count = mTerminalDiscountService.queryTerminalDiscountListCount(companyId,terminalDiscountListDto);
		PagingData<MTerminalDiscount> pagingData = new PagingData<>();
		pagingData.setData(channelDiscountList);
		pagingData.setRecordsTotal(count);
		return new MTerminalDiscountMgtExcution<PagingData<MTerminalDiscount>>(pagingData, MTerminalDiscountMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加优惠政策", nickname = "terminalDiscountAdd", notes = "", tags={ "@石匡" })
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody TerminalDiscountAddDto addDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		if(addDto.getEndTime().before(addDto.getStartTime())){
			return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.TIME);
		}
		int num = mTerminalDiscountService.queryTerminalDiscountCountByName(companyId,addDto.getName());
		if (num > 0){
			return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.EXIST);
		}

		num = mTerminalDiscountService.findTime(companyId,addDto.getProductId(),addDto.getStartTime(),addDto.getEndTime());
		if(num > 0){
				return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.FAILURES);
		}else{
			num = mTerminalDiscountService.addTerminalDiscountCount(companyId,addDto);
			if (num < 1){
				return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.FAILURE);
			}
		}
		return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "删除优惠政策", nickname = "terminalDiscountDelete", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="优惠政策id",paramType="query",dataType="string",required=true)
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="优惠政策id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		int num = mTerminalDiscountService.deleteTerminalDiscountCount(companyId,id);
		if (num < 1){
			return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.FAILURE);
		}
		return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "编辑优惠政策", nickname = "terminalDiscountEdit", notes = "", tags={ "@石匡" })
    @PostMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody TerminalDiscountEditDto editDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		if(editDto.getEndTime().before(editDto.getStartTime())){
			return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.TIME);
		}
		int num = mTerminalDiscountService.queryIsExist(companyId,editDto.getName(),editDto.getId());
		if (num > 0){
			return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.EXIST);
		}
		num = mTerminalDiscountService.findTimes(companyId,editDto.getProductId(),editDto.getStartTime(),editDto.getEndTime(),editDto.getId());
		if(num >0){
			return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.FAILURES);
		}else {
			num = mTerminalDiscountService.editTerminalDiscountCount(companyId, editDto);
			if (num < 1) {
				return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.FAILURE);
			}
		}
		return new MTerminalDiscountMgtExcution<Void>(null, MTerminalDiscountMgtEnum.SUCCESS);
	}

}
